package cc.ok200.muzhi;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

import com.alivc.live.annotations.AlivcLiveMode;
import com.alivc.live.pusher.AlivcAudioAACProfileEnum;
import com.alivc.live.pusher.AlivcFpsEnum;
import com.alivc.live.pusher.AlivcLiveBase;
import com.alivc.live.pusher.AlivcLiveBaseListener;
import com.alivc.live.pusher.AlivcLivePushConfig;
import com.alivc.live.pusher.AlivcLivePushConstants;
import com.alivc.live.pusher.AlivcLivePusher;
import com.alivc.live.pusher.AlivcPreviewOrientationEnum;
import com.alivc.live.pusher.AlivcResolutionEnum;
import com.alivc.live.pusher.AlivcSnapshotListener;
import com.alivc.live.pusher.AlivcVideoEncodeGopEnum;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ScreenRecorder {

    static String TAG = "ScreenRecorder";

    static Context mContext;

    static AlivcLivePushConfig config = new AlivcLivePushConfig();

    public static void init(Context context) {
        mContext = context;
        AlivcLiveBase.setListener(new AlivcLiveBaseListener() {
            @Override
            public void onLicenceCheck(AlivcLivePushConstants.AlivcLiveLicenseCheckResultCode result, String reason) {
                Log.e(TAG, "onLicenceCheck: " + result + ", " + reason);
            }
        });
        AlivcLiveBase.registerSDK();
    }

    public static void onMediaProjectionPermissionResultData(Intent data) {
        config.setMediaProjectionPermissionResultData(data);
    }

    public static void push() {
        AlivcLivePusher pusher = new AlivcLivePusher();
        pusher.init(mContext, config);

        pusher.snapshot(1, 1, new AlivcSnapshotListener() {
            @Override
            public void onSnapshot(Bitmap bmp) {

                String dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss-SS").format(new Date());
                File f = new File(mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "snapshot-" + dateFormat + ".png");
                if (f.exists()) {
                    f.delete();
                }
                try {
                    FileOutputStream out = new FileOutputStream(f);
                    bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
